package com.saturn.action.sldb.emergency;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.saturn.app.utils.DateUtils;
import com.saturn.app.web.IAction;
import com.saturn.app.web.IView;
import com.saturn.app.web.view.JspErrorView;
import com.saturn.app.web.view.JspView;
import com.saturn.auth.Organization;
import com.saturn.auth.User;
import com.saturn.sldb.Emergency;
import com.saturn.sldb.PersonState;
import com.saturn.sldb.Reconciliation;

public class ApplyEmergencyAction implements IAction{
	@Override
	public IView execute(HttpServletRequest request,
			HttpServletResponse response) {
		String id = request.getParameter("id");
		String type = request.getParameter("type"); //获取要申报的类型

		String note = request.getParameter("note");
		if("".equals(note) || note == null){
			note = "";
		}
		User user = (User) request.getSession().getAttribute("authUser");
		Emergency emergency = Emergency.get(id);
		User userOragantion = User.getOrganizationsByOrganizationId(user.getId());
		
		PersonState personState = new PersonState(null, id, user.getId(),
				user.getName(), DateUtils.getSystemTime(), emergency.getName(),
				emergency.getIdentify(), "", note, emergency.getCreaterDepartment(), "临时救助");

		if ("1".equals(type)) { // 乡镇街-申报
			String files = emergency.getFiles();
			String [] filesStr= files.split(";");
			if(emergency.getMoney() != null && "".equals(emergency.getMoney()) 
					 && !"".equals(emergency.getAudit()) && emergency.getAudit() != null){
				return new JspView("/app/sldb/emergency/approval/error.jsp");
			} else if(filesStr.length<5){
				return new JspView("/app/sldb/emergency/approval/errorFiles.jsp");
			}else{
				emergency.setState("待审批");
				if (Emergency.editToState(emergency) == 1) {

					personState.setState(emergency.getState());
					PersonState.add(personState);
					return new JspView("/app/sldb/emergency/approval/show.jsp");
				} else {
					return new JspErrorView("信息更新失败");
				}
			}
		} else if ("2".equals(type)) { // 乡镇街-完成
			emergency.setState("待核对");
			String files = emergency.getFiles();
			if("".equals(files) ||files== null){
				return new JspView("/app/sldb/emergency/apply/errorFiles.jsp");
			}else{
				if (Emergency.editToState(emergency) == 1) {
					personState.setState(emergency.getState());
					PersonState.add(personState);
					return new JspView("/app/sldb/emergency/apply/show.jsp");
				} else {
					return new JspErrorView("信息更新失败");
				}
			}
		} else if ("3".equals(type)) { // 乡镇街临时专项付款
			emergency.setState("已付款");
			emergency.setSubject("临时专项");
			String subject = emergency.getSubject();
			//String department = emergency.getCreaterDepartment();
			String organizationId = userOragantion.getOrganizationId();
			Organization organization = Organization.get(organizationId);
			String department = organization.getName();
			String typeStr = "临时救助";
			Reconciliation reg = Reconciliation.getBycondition(subject, department, typeStr);
			if("".equals(reg) || reg == null){
				return new JspView("/app/sldb/total/error.jsp");
			}else{
				String money = emergency.getMoney();
				int moneyInt = Integer.parseInt(money)+Integer.parseInt(reg.getSurplus());
				String moneyStr = Integer.toString(moneyInt);
				reg.setSurplus(moneyStr);
				String total = reg.getTotal();
				String surplus = reg.getSurplus();
				int balance = Integer.parseInt(total)-Integer.parseInt(surplus);
				String balanceStr = Integer.toString(balance);
				reg.setBalance(balanceStr);
			if (Emergency.editToState(emergency) == 1) {
				personState.setState(emergency.getState());
				PersonState.add(personState);
				Reconciliation.edit(reg);
				return new JspView("/app/sldb/account/country.jsp");
			} else {
				return new JspErrorView("信息更新失败");
			}
			}
		} else if ("11".equals(type)) { // 乡镇街结余调剂付款
			emergency.setState("已付款");
			emergency.setSubject("结余调剂");
			String subject = emergency.getSubject();
			//String department = emergency.getCreaterDepartment();
			String organizationId = userOragantion.getOrganizationId();
			Organization organization = Organization.get(organizationId);
			String department = organization.getName();
			String typeStr = "临时救助";
			Reconciliation reg = Reconciliation.getBycondition(subject, department, typeStr);
			if("".equals(reg) || reg == null){
				return new JspView("/app/sldb/total/error.jsp");
			}else{
				String money = emergency.getMoney();
				int moneyInt = Integer.parseInt(money)+Integer.parseInt(reg.getSurplus());
				String moneyStr = Integer.toString(moneyInt);
				reg.setSurplus(moneyStr);
				String total = reg.getTotal();
				String surplus = reg.getSurplus();
				int balance = Integer.parseInt(total)-Integer.parseInt(surplus);
				String balanceStr = Integer.toString(balance);
				reg.setBalance(balanceStr);
			if (Emergency.editToState(emergency) == 1) {
				personState.setState(emergency.getState());
				PersonState.add(personState);
				Reconciliation.edit(reg);
				return new JspView("/app/sldb/account/country.jsp");
			} else {
				return new JspErrorView("信息更新失败");
			}
			}
		}else if ("4".equals(type)) { 
			
		if("21".equals(userOragantion.getOrganizationId())){//如果乡镇街申报状态为待付款、如民政局申报状态为待支付
			emergency.setState("未支付");
			if(emergency.getAudit() == null || "".equals(emergency.getAudit())){
				return new JspView("/app/sldb/approval/pending/error.jsp");
			} else{
				if (Emergency.editToState(emergency) == 1) {

					personState.setState(emergency.getState());
					PersonState.add(personState);
					return new JspView("/app/sldb/approval/pending/show.jsp");
				}
			}
			
		}else{
			if("待付款".equals(Emergency.get(id).getState())){
				return new JspView("/app/sldb/account/error.jsp");
				}else{
				emergency.setState("待付款");
				if (Emergency.editToState(emergency) == 1) {

					personState.setState(emergency.getState());
					PersonState.add(personState);
					return new JspView("/app/sldb/account/country.jsp");
					}
				}
			}
				return new JspErrorView("信息更新失败");
			
		}else if ("15".equals(type)) { 
			emergency.setState("待付款");
			if("待付款".equals(Emergency.get(id).getState())){
				return new JspView("/app/sldb/account/error.jsp");
			}else{
				String subject = emergency.getSubject();
				String organizationId = userOragantion.getOrganizationId();
				Organization organization = Organization.get(organizationId);
				String department = organization.getName();
				String typeStr = "临时救助";
				Reconciliation reg = Reconciliation.getBycondition(subject, department, typeStr);
				if("".equals(reg) || reg == null){
					return new JspView("/app/sldb/total/error.jsp");
				}else {
					//获取申报救助金额
					String money = emergency.getMoney();
					//支出金额减掉付款金额
					int moneyInt = Integer.parseInt(reg.getSurplus())-Integer.parseInt(money);
					String moneyStr = Integer.toString(moneyInt);
					reg.setSurplus(moneyStr);
					//当前总金额加上付款金额
					int totalInt = Integer.parseInt(reg.getTotal())+Integer.parseInt(money);
					String totalStr = Integer.toString(totalInt);
					reg.setTotal(totalStr);
					String total = reg.getTotal();
					String surplus = reg.getSurplus();
					int balance = Integer.parseInt(total)-Integer.parseInt(surplus);
					String balanceStr = Integer.toString(balance);
					reg.setBalance(balanceStr);
					if (Emergency.editToState(emergency) == 1) {
						personState.setState(emergency.getState());
						PersonState.add(personState);
						Reconciliation.edit(reg);
						return new JspView("/app/sldb/account/cancelByxz.jsp");
					}else {
						return new JspErrorView("信息更新失败");
					}
				}
			}
			
		}else if ("5".equals(type)) { 
			emergency.setState("待审核");
			if (Emergency.editToState(emergency) == 1) {

				personState.setState(emergency.getState());
				PersonState.add(personState);
				return new JspView("/app/sldb/check/show.jsp");
			} else {
				return new JspErrorView("信息更新失败");
			}
		}else if ("6".equals(type)) { 
			emergency.setState("未支付");
			if("未支付".equals(Emergency.get(id).getState())){
				return new JspView("/app/sldb/account/error.jsp");
			}else{
				String subject = emergency.getSubject();
				String organizationId = userOragantion.getOrganizationId();
				Organization organization = Organization.get(organizationId);
				String department = organization.getName();
				String typeStr = "临时救助";
				Reconciliation reg = Reconciliation.getBycondition(subject, department, typeStr);
				if("".equals(reg) || reg == null){
					return new JspView("/app/sldb/total/error.jsp");
				}else {
					//获取申报救助金额
					String money = emergency.getMoney();
					//支出金额减掉付款金额
					int moneyInt = Integer.parseInt(reg.getSurplus())-Integer.parseInt(money);
					String moneyStr = Integer.toString(moneyInt);
					reg.setSurplus(moneyStr);
					//当前总金额加上付款金额
					int totalInt = Integer.parseInt(reg.getTotal())+Integer.parseInt(money);
					String totalStr = Integer.toString(totalInt);
					reg.setTotal(totalStr);
					String total = reg.getTotal();
					String surplus = reg.getSurplus();
					int balance = Integer.parseInt(total)-Integer.parseInt(surplus);
					String balanceStr = Integer.toString(balance);
					reg.setBalance(balanceStr);
					if (Emergency.editToState(emergency) == 1) {
						personState.setState(emergency.getState());
						PersonState.add(personState);
						Reconciliation.edit(reg);
						return new JspView("/app/sldb/account/cancel.jsp");
					} else {
						return new JspErrorView("信息更新失败");
					}
				}
			}
			
		}else if ("7".equals(type)) { 
			emergency.setState("已支付");
			if("已支付".equals(Emergency.get(id).getState())){
				return new JspView("/app/sldb/account/error.jsp");
			}else{
				emergency.setSubject("临时专项");
				String subject = emergency.getSubject();
				//String department = emergency.getCreaterDepartment();
				String organizationId = userOragantion.getOrganizationId();
				Organization organization = Organization.get(organizationId);
				String department = organization.getName();
				String typeStr = "临时救助";
				Reconciliation reg = Reconciliation.getBycondition(subject, department, typeStr);
				if("".equals(reg) || reg == null){
					return new JspView("/app/sldb/total/error.jsp");
				}else{
					String money = emergency.getMoney();
					int moneyInt = Integer.parseInt(money)+Integer.parseInt(reg.getSurplus());
					String moneyStr = Integer.toString(moneyInt);
					reg.setSurplus(moneyStr);
					String total = reg.getTotal();
					String surplus = reg.getSurplus();
					int balance = Integer.parseInt(total)-Integer.parseInt(surplus);
					String balanceStr = Integer.toString(balance);
					reg.setBalance(balanceStr);
					if (Emergency.editToState(emergency) == 1) {
						personState.setState(emergency.getState());
						PersonState.add(personState);
						Reconciliation.edit(reg);
						return new JspView("/app/sldb/account/center.jsp");
					} else {
						return new JspErrorView("信息更新失败");
					}
				}
				
			}
		} else if ("13".equals(type)) { 
			emergency.setState("已支付");
			if("已支付".equals(Emergency.get(id).getState())){
				return new JspView("/app/sldb/account/error.jsp");
			}else{
				emergency.setSubject("结余调剂");
				String subject = emergency.getSubject();
				//String department = emergency.getCreaterDepartment();
				String organizationId = userOragantion.getOrganizationId();
				Organization organization = Organization.get(organizationId);
				String department = organization.getName();
				String typeStr = "临时救助";
				Reconciliation reg = Reconciliation.getBycondition(subject, department, typeStr);
				if("".equals(reg) || reg == null){
					return new JspView("/app/sldb/total/error.jsp");
				}else{
					String money = emergency.getMoney();
					int moneyInt = Integer.parseInt(money)+Integer.parseInt(reg.getSurplus());
					String moneyStr = Integer.toString(moneyInt);
					reg.setSurplus(moneyStr);
					String total = reg.getTotal();
					String surplus = reg.getSurplus();
					int balance = Integer.parseInt(total)-Integer.parseInt(surplus);
					String balanceStr = Integer.toString(balance);
					reg.setBalance(balanceStr);
				if (Emergency.editToState(emergency) == 1) {
					personState.setState(emergency.getState());
					PersonState.add(personState);
					Reconciliation.edit(reg);
					return new JspView("/app/sldb/account/center.jsp");
				} else {
					return new JspErrorView("信息更新失败");
				}
			}
			}
		} else if ("8".equals(type)) { //乡镇街待付款
			emergency.setState("待付款");
			String files = emergency.getFiles();
			String [] filesStr= files.split(";");
			int money = Integer.parseInt(emergency.getMoney());
			if(money > 1000 ){
				return new JspView("/app/sldb/emergency/apply/errorByMoney.jsp");
			}else {
			if(filesStr.length<5){
				return new JspView("/app/sldb/emergency/approval/errorFiles.jsp");
			}
			if(emergency.getMoney() == null || "".equals(emergency.getMoney()) || emergency.getAudit() == null || "".equals(emergency.getAudit())){
				return new JspView("/app/sldb/emergency/approval/error.jsp");
			} else{
				if (Emergency.editToState(emergency) == 1) {

					personState.setState(emergency.getState());
					PersonState.add(personState);
					return new JspView("/app/sldb/emergency/approval/show.jsp");
				} else {
					return new JspErrorView("信息更新失败");
				}
			}
			}
		} else if ("9".equals(type)) { //乡镇街待付款
			emergency.setState("已作废");
			if (Emergency.editToState(emergency) == 1) {

				personState.setState(emergency.getState());
				PersonState.add(personState);
				return new JspView("/app/sldb/emergency/approval/show.jsp");
			} else {
				return new JspErrorView("信息更新失败");
			}
		}else if ("14".equals(type)) { //民政局取消审批
			emergency.setState("待审批");
			if (Emergency.editToState(emergency) == 1) {

				personState.setState(emergency.getState());
				PersonState.add(personState);
				return new JspView("/app/sldb/approval/cancel/show.jsp");
			} else {
				return new JspErrorView("信息更新失败");
			}
		}else if ("17".equals(type)) { //民政局取消审批
			emergency.setState("待审核");
			if (Emergency.editToState(emergency) == 1) {

				personState.setState(emergency.getState());
				PersonState.add(personState);
				return new JspView("/app/sldb/emergency/cancel/show.jsp");
			} else {
				return new JspErrorView("信息更新失败");
			}
		}else { // 民政局审批人员-驳回
			String remarks = request.getParameter("remarks");
			emergency.setState("创建");
			emergency.setRemarks(remarks);
			personState.setNote(remarks);
			if (Emergency.editToState(emergency) == 1) {
				personState.setState(emergency.getState());
				PersonState.add(personState);
				return new JspView("/app/sldb/approval/pending/show.jsp");
			} else {
				return new JspErrorView("信息更新失败");
			}
		}
	}
	@Override
	public String requestMapping() {
		return "/app/sldb/emergency/applyEmergency.action";
	}
}
